﻿package  com.hadron.graphics.rectangle {
	import temple.core.CoreSprite;

	import flash.display.Graphics;
	import flash.display.LineScaleMode;
	import flash.geom.ColorTransform;
	
	/**
	 * This is a wrapper class for basic rectangle sprite
	 * @author Hadron
	 */
	public class HadronRectangle extends CoreSprite
	{	
		/** @private */
		private var _width:Number;
		/** @private */
		private var _height:Number;
		/** @private */
		private var _color:uint;
		/** @private */
		private var _alpha:Number;
		/** @private */
		private var _cornerRadius:uint;
		/** @private */
		private var _stroke:Number;
		/** @private */
		private var _strokeColor:uint;
		
		public function HadronRectangle( width:Number = 100,
									   height:Number = 100,
									   color:uint = 0x000000,
									   alpha:Number = 1,
									   cornerRadius:uint = 0,
									   stroke:Number = 0,
									   strokeColor:uint = 0xFFFFFF) 
		{
			this._width = width;
			this._height = height;
			this._color = color;
			this._stroke = stroke;
			this._cornerRadius = cornerRadius;
			this._alpha = alpha;
			this._strokeColor = strokeColor;
			update();
		}
		
		private function update():void
		{
			var g:Graphics = this.graphics;
			g.clear();
			with (g)
			{
				beginFill(_color, _alpha);
				if (_stroke != 0) 
					g.lineStyle(_stroke, _strokeColor, 1, true, LineScaleMode.NONE);
				(_cornerRadius != 0) ? drawRoundRect(0, 0, _width, _height, _cornerRadius, _cornerRadius) : drawRect(0, 0, _width, _height);
				endFill();
			}
		}
		
		override public function set width(value:Number):void
		{
			_width = value;
			update();
			super.width = value;
		}
		
		override public function set height(value:Number):void
		{
			_height = value;
			update();
			super.height = value;
		}
		
		public function set color(value:uint):void
		{
			_color = value;
			//transform color
			var newColor:ColorTransform = this.transform.colorTransform;
			newColor.color = _color;
			this.transform.colorTransform = newColor;
		}
		
		public function get color():uint
		{
			return _color;
		}
		
		public function set cornerRadius(value:uint):void
		{
			_cornerRadius = value;
			update();
		}
		
		public function get cornerRadius():uint
		{
			return _cornerRadius;
		}
	}
}